/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/


#ifndef B2_RENDER_H
#define B2_RENDER_H

/* b2d stuff */
#include "Box2D.h"


/* this stuff */

#include "b2d_common.h"


#define GLCOLOR(RED,GREEN,BLUE,ALPHA)					\
	argcount = 0;								\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));	\
	atom_setsym(atomArray+(argcount++),gensym("glcolor"));	\
	atom_setfloat(atomArray+(argcount++),RED);			\
	atom_setfloat(atomArray+(argcount++),GREEN);			\
	atom_setfloat(atomArray+(argcount++),BLUE);			\
	atom_setfloat(atomArray+(argcount++),ALPHA);			\
	outlet_list(main_outlet,0L,argcount,atomArray);\

#define GLBEGIN(TYPE) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glbegin"));	\
	atom_setsym(atomArray+(argcount++), gensym(TYPE)); \
	outlet_list(main_outlet,0L,argcount,atomArray);		\
	
#define GLEND \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glend"));	\
	outlet_list(main_outlet,0L,argcount,atomArray);		\

#define GLVERTEX(X,Y) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glvertex"));	\
	atom_setfloat(atomArray+(argcount++), X); \
	atom_setfloat(atomArray+(argcount++), Y); \
	outlet_list(main_outlet,0L,argcount,atomArray);	\
	
#define GLENABLE(MODE) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glenable"));	\
	atom_setsym(atomArray+(argcount++), gensym(MODE));	\
	outlet_list(main_outlet,0L,argcount,atomArray);	\

#define GLDISABLE(MODE) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("gldisable"));	\
	atom_setsym(atomArray+(argcount++), gensym(MODE));	\
	outlet_list(main_outlet,0L,argcount,atomArray);	\
	
#define GLBLENDFUNC(SRC,DST) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glblendfunc"));	\
	atom_setsym(atomArray+(argcount++), gensym(SRC));	\
	atom_setsym(atomArray+(argcount++), gensym(DST));	\
	outlet_list(main_outlet,0L,argcount,atomArray);	\
	
#define GLPOINTSIZE(SIZE) \
	argcount = 0;	\
	atom_setsym(atomArray+(argcount++),gensym("debugdraw"));\
	atom_setsym(atomArray+(argcount++), gensym("glpointsize"));	\
	atom_setfloat(atomArray+(argcount++), SIZE); \
	outlet_list(main_outlet,0L,argcount,atomArray);		\
	



//struct b2AABB;

// This class implements debug drawing callbacks that are invoked
// inside b2World::Step.
class DebugDraw : public b2DebugDraw
{

public:
	void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

	void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

	void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);

	void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);

	void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);

	void DrawXForm(const b2XForm& xf);

	void SetOutlet(t_object * oulet);
	
	void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);

	void DrawString(int x, int y, const char* string, ...);
	
	void DrawAABB(b2AABB* aabb, const b2Color& color);

	
	private:
	
	
	t_object *			main_outlet;
	
	long				argcount;
	
	/* for the macros */
	/* pre allocate the arrays for speed's sake */
	t_atom				atomArray[1024];
	
};


#endif
